package com.xzlf.service.impl;

import com.xzlf.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Override
    public String getInfo() {
        ServiceInstance si = loadBalancerClient.choose("application-service");
        RestTemplate restTemplate = new RestTemplate();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("http://").append(si.getHost()).append(":").append(si.getPort());
        ResponseEntity<String> response = restTemplate.exchange(stringBuffer.toString(), HttpMethod.GET, null, String.class);
        return response.getBody();
    }
}
